package com.wwwarehouse.usercenter.fragment.chain_triangles;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.eventbus_event.ScanCodeEvent;
import com.wwwarehouse.common.tools.QRCodeUtils;
import com.wwwarehouse.usercenter.R;

import de.greenrobot.event.EventBus;


/**
 * Created by xiaoke.ni on 2017/8/31.
 */

public class TCF2FFragment extends BaseFragment implements View.OnClickListener {

    private View mView;
    private ImageView mIvQrCode;
    private TextView mContent1Tv;
    private TextView mContent2Tv;
    private String inviteCode;
    private String inviteUrl;
    private int authType;
    private int from;//0建立1管理

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getActivity(), R.layout.fragment_tc_f2f, null);
        return mView;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initEvent();
    }

    private void initView() {
        mIvQrCode = (ImageView) mView.findViewById(R.id.iv_qr_code);
        mContent1Tv = (TextView) mView.findViewById(R.id.content1_tv);
        mContent2Tv = (TextView) mView.findViewById(R.id.content2_tv);
    }

    private void initEvent() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof TCF2FFragment) {
            mActivity.setTitle(getResources().getString(R.string.set_triangular_management));
        }
    }

    @Override
    public void requestDatas() {
        Bundle bundle = getArguments();
        from = bundle.getInt("from",0);
        inviteCode = bundle.getString("inviteCode");
        inviteUrl = bundle.getString("inviteUrl");
        authType = bundle.getInt("authType");
        refreshQRCode(inviteUrl);
        if(authType == 20)
            mContent2Tv.setText(getResources().getString(R.string.sweep_code_level_managers));
        if(authType == 21)
            mContent2Tv.setText(getResources().getString(R.string.sweep_code_level_monitoring));
    }

    private void refreshQRCode(String inviteUrl){
        Bitmap bitmap = QRCodeUtils.createQRCode(inviteUrl);
        mIvQrCode.setImageBitmap(bitmap);

    }


    //面对面邀请
    public void onEventMainThread(ScanCodeEvent event) {
        String message = event.getResult();
        if(authType == 20){
            TriangulationChainFragment.hasAT = true;
        }else{
            TriangulationChainFragment.hasSV = true;
        }

        TCSuccessFragment tcSuccessFragment = new TCSuccessFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", Constant.TYPE_F2F);
        bundle.putInt("from", from);//0建立1管理
        bundle.putString("message",message);
        tcSuccessFragment.setArguments(bundle);
        popFragment();
        pushFragment(tcSuccessFragment);
    }

}
